package com.edu.exam.entity.grading.integration;

import jakarta.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 数据冲突记录实体类
 *
 * <p>所属模块：B-06 成绩管理模块</p>
 * <p>对应数据库表：data_conflict_record</p>
 *
 * <p>存储数据冲突检测和解决记录</p>
 *
 * @author G-09 Module Designer
 * @version 2.0
 * @since 2025-11-10
 */
@Entity
@Data
@EqualsAndHashCode(callSuper = false)
public class DataConflictRecord {

    @Id
    @Column(name = "conflict_id", length = 64)
    private String conflictId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "task_id", referencedColumnName = "task_id", nullable = false, foreignKey = @ForeignKey(name = "fk_data_conflict_record_task"))
    private IntegrationTask integrationTask;

    @Column(name = "record_id", length = 64, nullable = false)
    private String recordId;

    @Column(name = "conflict_type", length = 32, nullable = false)
    private String conflictType;

    @Column(name = "conflict_field", length = 64)
    private String conflictField;

    @Column(name = "original_value", columnDefinition = "TEXT")
    private String originalValue;

    @Column(name = "existing_value", columnDefinition = "TEXT")
    private String existingValue;

    @Column(name = "conflict_description", columnDefinition = "TEXT")
    private String conflictDescription;

    @Column(name = "resolution_strategy", length = 32)
    private String resolutionStrategy;

    @Column(name = "resolved_value", columnDefinition = "TEXT")
    private String resolvedValue;

    @Column(name = "resolution_status", length = 32, nullable = false)
    private String resolutionStatus;

    @Column(name = "resolved_by", length = 64)
    private String resolvedBy;

    @Column(name = "resolved_time")
    private LocalDateTime resolvedTime;

    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @UpdateTimestamp
    @Column(name = "updated_time")
    private LocalDateTime updatedTime;

    /**
     * 获取关联的任务ID（为了便于查询）
     */
    @Transient
    public String getTaskId() {
        return integrationTask != null ? integrationTask.getTaskId() : null;
    }

}
